package com.tj.jinzao.activity;


import android.app.Activity;
import android.content.Intent;
import android.text.Html;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseActivity;
import com.tj.jinzao.base.IBaseView;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.model.Address;
import com.tj.jinzao.model.Goods;
import com.tj.jinzao.presenter.AddressPresenter;
import com.tj.jinzao.utils.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 早币商城页面
 * Created by wjy on 2019/1/5.
 */
public class GoodsDetailActivity extends BaseActivity implements IBaseView<List<Address>> {

    @BindView(R.id.iv_goods)
    ImageView mImageView;
    @BindView(R.id.tv_desc)
    TextView mDescView;
    @BindView(R.id.tv_name)
    TextView mNameView;
    @BindView(R.id.tv_score)
    TextView mScoreView;
    @BindView(R.id.tv_address)
    TextView mAddressView;

    private AddressPresenter mPresenter;

    private Goods mGoods;
    private Address mAddress;

    @Override
    protected void setupView() {
        super.setupView();
        ViewGroup.LayoutParams layoutParams = mImageView.getLayoutParams();
        layoutParams.width = Constant.SCREEN_WIDTH / 2;
        layoutParams.height = Constant.SCREEN_WIDTH / 2 * 229 / 209;
        mImageView.setLayoutParams(layoutParams);
        mGoods = (Goods) getIntent().getSerializableExtra("goods");
        if (mGoods == null) return;
        mNameView.setText(mGoods.getTitle());
        mDescView.setText(Html.fromHtml(mGoods.getContent()));
        mScoreView.setText(mGoods.getScore() + "积分");
        Glide.with(this).load(Constant.IMAGE_PREFIX + mGoods.getImgurl()).into(mImageView);
        mPresenter = new AddressPresenter(this);
        mPresenter.getAddressList();
    }

    @OnClick(R.id.rl_address_layout)
    void chooseAddress() {
        startActivityForResult(new Intent(this, AddressActivity.class), 100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
            try {
                mAddress = (Address) data.getSerializableExtra("address");
                if (mAddress != null) {
                    mAddressView.setText(mAddress.getAddress());
                }
            } catch (Exception e) {
            }
        }
    }

    @OnClick(R.id.bt_exchange)
    public void exchange() {
        if(mAddress == null){
            ToastUtils.showToast(this,"请输入地址");
        }else {
            mPresenter.exchange(mGoods.getId(), mAddress.getId());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) mPresenter.detachView();
    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_goods_detail;
    }

    @Override
    protected int getLeftButtonResId() {
        return R.mipmap.ic_back;
    }

    @Override
    public Activity getContext() {
        return this;
    }

    @Override
    public void onRequestSuccess(List<Address> addresses) {
        for (Address address : addresses) {
            if (address.isIsDefault()) {
                mAddress = address;
                mAddressView.setText(address.getAddress());
                break;
            }
        }
    }

    @Override
    public void onRequestNoData(String message) {

    }

    @Override
    public void onRequestError(String error_message) {

    }
}
